package com.xiyou.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiyou.weblog.common.domain.dos.CategoryDo;
import com.xiyou.weblog.common.domain.dos.CategoryDo;

import java.time.LocalDate;
import java.util.Objects;

/**
 * @author xiyou
 * @description 文章分类
 */
public interface CategoryMapper extends BaseMapper<CategoryDo> {

    default CategoryDo selectByName(String categoryName) {
        //构建查询条件
        LambdaQueryWrapper<CategoryDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CategoryDo::getName, categoryName);

        //执行
        return selectOne(wrapper);
    }

    /**
     * 分类分页查询
     */
    default Page<CategoryDo> selectPageList(Long current, long size, String name, LocalDate startDate, LocalDate endDate) {
        // 分页对象
        Page<CategoryDo> page = new Page<>(current, size);
        // 构建查询条件
        LambdaQueryWrapper<CategoryDo> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .like(Objects.nonNull(name),CategoryDo::getName,name)//参数一：判断为不为空，为空直接滤过
                .ge(Objects.nonNull(startDate),CategoryDo::getCreateTime,startDate)//大于
                .le(Objects.nonNull(endDate),CategoryDo::getCreateTime,endDate)//小于
                .orderByDesc(CategoryDo::getCreateTime);// 根据时间倒序排序

        return selectPage(page, wrapper);
    }
}
